<?php

namespace App\Http\Controllers\Admin\Framework\Manage;

use App\Http\Controllers\Admin\ParentController;
use App\Master\Framework\Extend\DataUtil;
use App\Master\Framework\View\AdminData;
use App\Models\Admin\Framework\AdminRoleModel;
use App\Utils\AppResult;
use Illuminate\Http\Request;

class RoleController extends ParentController
{

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    public function index(Request $request)
    {
        if ($request->isMethod('post')) {
            $params = $request->all();
            $query  = AdminRoleModel::query()->select(['id', 'name', 'group_id', 'menu_ids', 'menu_half_ids', 'status', 'is_delete', 'description', 'created_at', 'updated_at'])
                ->whereIn('status', [1, 2])->where('is_show', 1)->where('is_delete', 0);

            $query->with([
                'group'
            ]);

            if (!empty($params['keyword'])) {
                $query->where('name', 'like', "%{$params['keyword']}%");
            }

            if (isset($params['status'])) {
                $query->where('status', $params['status']);
            }

            if ($orderBy = AdminData::orderBy()) {
                foreach ($orderBy as $order){
                    $query->orderBy($order['sort'], $order['orderBy']);
                }
            }

            return AppResult::response200('success', $query->paginate($params['size'] ?? 15));
        } else {
            return view('admin.framework.manage.role.index');
        }
    }

    public function insert(Request $request)
    {
        $params = $request->all();
        if (empty($params['name'])) {
            return AppResult::response203('请填写角色名称！');
        }

        // 初始化空数据
        $form               = DataUtil::field($params,['status']);
        $form['menu_ids']     = json_encode($form['menu_ids']);
        $form['menu_half_ids']     = json_encode($form['menu_half_ids']);
        $form['group_id']     = !empty($params['group_id'][0]) ? $params['group_id'][0] : 0;
        $form['status']     = $params['status'] == 'true' ? 1 : 2;
        $form['created_at'] = time();
        if (!AdminRoleModel::query()->insert($form)){
            return AppResult::response201('创建失败');
        }
        return AppResult::response200('创建成功');
    }

    public function update(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }
        if (empty($params['name'])) {
            return AppResult::response203('请填写角色名称！');
        }

        // 初始化空数据
        $form               = DataUtil::field($params,['status']);
        $form['menu_ids']     = json_encode($form['menu_ids']);
        $form['menu_half_ids']     = json_encode($form['menu_half_ids']);
        $form['group_id']     = !empty($params['group_id'][0]) ? $params['group_id'][0] : 0;
        $form['status']     = $params['status'] == 'true' ? 1 : 2;
        $form['updated_at'] = time();
        if (!AdminRoleModel::query()->where('id',$params['id'])->update($form)){
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    public function status(Request $request)
    {
        $params = $request->all();
        if (empty($params['id']) || !isset($params['status'])) {
            return AppResult::response203('操作超时！');
        }

        $form['status']  = $params['status'] == 1 ? 2 : 1;
        $form['updated_at'] = time();
        if (!AdminRoleModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    public function remove(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }

        $form['is_delete']  = 1;
        $form['updated_at'] = time();
        if (!AdminRoleModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }
}